//
//  HXSoloModeViewController.m
//  HongxuanBluetooth
//
//  Created by 广州弘轩科技发展有限公司 on 2023/8/11.
//

#import "HXSoloModeViewController.h"

@interface HXSoloModeViewController ()
@property (strong,nonatomic) UILabel *bluetoothTimelb;
@property (strong, nonatomic) BlueToothManager *blueToothManager;//蓝牙
@property (strong,nonatomic) UIImageView * batterystatus;
@end

@implementation HXSoloModeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.fd_prefersNavigationBarHidden = YES;
    
    
    [self BottomFunction];
    [self dianliang];//获取电量
}
-(void)BottomFunction{
    
    UIImageView *tabr = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight-44, winWidth, 44)];
    [self.view addSubview:tabr];
    UIImageView *mainscreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 44)];
    mainscreen.image = [UIImage imageNamed:@"solodemomian"];
    [tabr addSubview:mainscreen];
    
    UIImageView * xinhao = [[UIImageView alloc] init];
    xinhao.image = [UIImage imageNamed:@"devicesignal"];
    [self.view addSubview:xinhao];
    [xinhao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(28);
        make.left.mas_equalTo(68);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(49);
    }];
    
    self.blueToothManager = [BlueToothManager shareInstance];
//    暂时定位信号值
    WeakSelf
    static int dian = 0;
    self.blueToothManager.RSSIBlock = ^(NSInteger rsii) {
        dian ++;
        if(rsii>=-50){
//            NSLog(@"信号0 -- -50");
            xinhao.image = [UIImage imageNamed:@"signalmian"];
        }
        if(rsii< -50&&rsii>=-60){
//            NSLog(@"信号-50 -- -60");
            xinhao.image = [UIImage imageNamed:@"signalmian3"];
        }
        if(rsii< -60&&rsii>-70){
//            NSLog(@"信号-60 -- -70");
            xinhao.image = [UIImage imageNamed:@"signalmian2"];
        }
        if(rsii<= -70&&rsii>-80){
//            NSLog(@"信号-60 -- -70");
            xinhao.image = [UIImage imageNamed:@"signalmian1"];
        }
        if(rsii<= -80){
//            NSLog(@"信号-80以上");
            xinhao.image = [UIImage imageNamed:@"signalmian0"];
        }
//        timelb.text = [NSString stringWithFormat:@"%ld",rsii];
        if(dian % 60 == 0){
            NSLog(@"第一分钟查询电量");
            [weakSelf dianliang];
        }
    };
    
    UIImageView * dianliang = [[UIImageView alloc] init];
    dianliang.image = [UIImage imageNamed:@"electricquantity"];
    self.batterystatus = dianliang;
    [self.view addSubview:dianliang];
    [dianliang mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(28);
        make.right.mas_equalTo(-68);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(44);
    }];
//  top44   237 68
    
    UILabel *timelb = [[UILabel alloc] init];
    timelb.textAlignment = NSTextAlignmentCenter;
    NSString *bluetoothTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"Bluetoothtime"];
    if(bluetoothTime.length>0){
        timelb.text = bluetoothTime;
    }else{
        timelb.text = @"00:00";
    }
    
    timelb.textColor = [UIColor whiteColor];
    timelb.font = [UIFont systemFontOfSize:90];
    self.bluetoothTimelb = timelb;
    [self.view addSubview:timelb];
     [timelb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(69);
        make.right.mas_equalTo(-69);
        make.top.mas_equalTo(tabr.mas_bottom).offset(28+44);
        make.height.mas_equalTo(68);
    }];
//    gridscreen
    UIImageView *gridscreenview = [[UIImageView alloc] init];
    gridscreenview.image = [UIImage imageNamed:@"gridscreen"];
    gridscreenview.userInteractionEnabled = YES;
    [self.view addSubview:gridscreenview];
    [gridscreenview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(timelb.mas_bottom).offset(55);
        make.left.mas_equalTo(46);
        make.width.height.mas_equalTo(150);
    }];
    
    UIButton *gridscreenbt = [UIButton buttonWithType:UIButtonTypeCustom];
    gridscreenbt.tag = 1;
    [gridscreenview addSubview:gridscreenbt];
    [gridscreenbt addTarget:self action:@selector(featureSelected:) forControlEvents:UIControlEventTouchUpInside];
    [gridscreenbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);

    }];
    
//    codescreen
    UIImageView *codescreenview = [[UIImageView alloc] init];
    codescreenview.image = [UIImage imageNamed:@"codescreen"];
    codescreenview.userInteractionEnabled = YES;
    [self.view addSubview:codescreenview];
    [codescreenview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(timelb.mas_bottom).offset(55);
        make.right.mas_equalTo(-46);
        make.width.height.mas_equalTo(150);
    }];
    
    UIButton *codescreenbt = [UIButton buttonWithType:UIButtonTypeCustom];
    codescreenbt.tag = 2;
    [codescreenview addSubview:codescreenbt];
    [codescreenbt addTarget:self action:@selector(featureSelected:) forControlEvents:UIControlEventTouchUpInside];
    [codescreenbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    UIImageView *statebj = [[UIImageView alloc] init];
    if([themeColors isEqualToString:@"default"]){
        statebj.backgroundColor = [UIColor blackColor];
    }else{
        statebj.backgroundColor = midnightRGB;
    }
    [self.view addSubview:statebj];
    [statebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-34);
        make.height.mas_equalTo(50);
    }];
#pragma mark 返回入口
    UIImageView *backview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back"]];
    backview.userInteractionEnabled = YES;
    [self.view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(36);
        make.left.mas_equalTo(26);
    }];
    
    UIButton *backbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:backbt];
    [backbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(backview);
    }];
    
    
#pragma mark 链接入口
    UIImageView *scanview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"detal"]];
    scanview.userInteractionEnabled = YES;
    [self.view addSubview:scanview];
    [scanview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(36);
        make.right.mas_equalTo(-26);
    }];
    
    UIButton *detalbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [detalbt addTarget:self action:@selector(detalbt) forControlEvents:UIControlEventTouchUpInside];
    [scanview addSubview:detalbt];
    [detalbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(scanview);
    }];
    
    
    UIImageView *bluetherview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bluether"]];
    bluetherview.userInteractionEnabled = YES;
    [self.view addSubview:bluetherview];
    [bluetherview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-52);
        make.height.mas_equalTo(13);
    }];

    UIImageView *solospecification = [[UIImageView alloc] init];
    solospecification.image = [UIImage imageNamed:@"solospecification"];
    [self.view addSubview:solospecification];
    [solospecification mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bluetherview.mas_top).offset(-32);
        make.height.mas_equalTo(37);
        make.width.mas_equalTo(204);
        make.centerX.mas_equalTo(self.view);
    }];
}
- (void)dianliang{
    
//    获取电量
    Byte send[] = {0x58,0x57,0x03,0x0B,0x00,0x00,0x00,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    [self.blueToothManager writeData:sendData];
    //    链接
    WeakSelf
    self.blueToothManager.datastrBlock = ^(NSString * _Nonnull datastr) {
        NSLog(@"datastr:%@",datastr);
        NSArray *Arr = [NSArray array];
        Arr = [datastr componentsSeparatedByString:@"0x585703"];
        NSLog(@"componentsSeparatedByString========%@",Arr);
        NSString *results2 = [Arr[1] substringToIndex:2];
        NSLog(@"results2:%@",results2);
        int dinal = [XHbluetoolUtils hexadecimalto10:results2];
        NSLog(@"dinal :%d",dinal);
//        电量：0-100
        if(dinal >= 0&& dinal<=20){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus0"];
        }
        if(dinal >20 && dinal<=40){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus1"];
        }
        if(dinal >40 && dinal<=60){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus2"];
        }
        if(dinal >60 && dinal<=80){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus3"];
        }
        if(dinal >80 && dinal< 100){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus"];
        }
    };
}
-(void)back{
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)detalbt{
    NSLog(@"跳转到detal界面");
    HXDetailViewController*jum = [[HXDetailViewController alloc] init];
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
-(void)featureSelected:(UIButton *)button{
    switch (button.tag) {
        case 1:
            NSLog(@"点击了gridscreen");
            [self solojump];
            break;
        case 2:
            NSLog(@"点击了codescreen");
            [self CodeScreen];
            break;
            
        default:
            break;
    }
}
-(void)solojump{

    HXGridScreenViewController*jum = [[HXGridScreenViewController alloc] init];
    jum.bluetoothtimeBlock = ^(NSString * _Nonnull time) {
        self.bluetoothTimelb.text = time;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"bluetoothTime" object:nil userInfo:@{@"time":time}];
    };
    jum.bluetoothTime = self.bluetoothTimelb.text;
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
    
}
-(void)CodeScreen{
//    HXCodeScreenViewController
    HXCodeScreenViewController*jum = [[HXCodeScreenViewController alloc] init];
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
    jum.bluetoothtimeBlock = ^(NSString * _Nonnull time) {
        self.bluetoothTimelb.text = time;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"bluetoothTime" object:nil userInfo:@{@"time":time}];
    };
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
